package cc.autoapi.pucong.auto.flow.provider.group.service;


import cc.autoapi.pucong.auto.flow.core.root.utils.UUID;
import cc.autoapi.pucong.auto.flow.persistence.utils.UserRequest;
import cc.autoapi.pucong.auto.flow.persistence.po.sys.GroupInfoPo;
import cc.autoapi.pucong.auto.flow.persistence.repository.node.NodeInfoService;
import cc.autoapi.pucong.auto.flow.persistence.utils.jdbc.DatabaseUtils;
import cc.autoapi.pucong.auto.flow.provider.group.convert.GroupInfoBeanConvert;
import cc.autoapi.pucong.auto.flow.provider.group.entity.GroupInfoInsertDto;
import cc.autoapi.pucong.auto.flow.provider.group.entity.GroupInfoSelectDto;
import cc.autoapi.pucong.auto.flow.provider.group.entity.GroupInfoUpdateDto;
import cc.autoapi.pucong.auto.flow.provider.group.entity.GroupInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 组信息 service实现类
 *
 * @author pucong
 * @url https://gitee.com/pucong
 * @create 2023-05-31
 */
@Service
@Slf4j
public class GroupInfoServiceImpl implements GroupInfoService {

    @Resource
    private NodeInfoService nodeInfoService;

    @Autowired
    private GroupInfoBeanConvert groupInfoBeanConvert;

    @Override
    public List<GroupInfoVo> getGroupInfoList(GroupInfoSelectDto dto) {
        List<GroupInfoPo> groupInfos = this.list(dto.getName(), dto.getGroupType());
        List<GroupInfoVo> groupInfoVos = groupInfoBeanConvert.do2vos(groupInfos);
        return groupInfoVos;
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int insertGroupInfo(GroupInfoInsertDto dto) {
        GroupInfoPo groupInfo = groupInfoBeanConvert.dto2do(dto);
        String uuid = UUID.uuid();
        groupInfo.setUuid(uuid);
        groupInfo.setCreateBy(UserRequest.getLoginUser().getUuid());
        groupInfo.setCreateTime(new Date());
        int i = save(groupInfo);
        return i;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int updateGroupInfoById(String id, GroupInfoUpdateDto dto) {
        GroupInfoPo groupInfoUpdate = groupInfoBeanConvert.dto2do(dto);
        groupInfoUpdate.setUuid(id);
        int i = updateById(groupInfoUpdate);
        return i;
    }

    @Override
    public int batchDeleteGroupInfoByIds(List<String> ids) {
        deleteByIds(ids);
        // 删除分组下的节点
        nodeInfoService.deleteByGroupIds(ids);
        return 0;
    }

    private int deleteByIds(List<String> ids) {
        if (ids == null || ids.size() < 1) {
            return 0;
        }
        StringBuffer sqlBf = new StringBuffer();
        sqlBf.append("delete from auto_flow_group_info where uuid in (");
        for (int i = 0; i < ids.size(); i++) {
            sqlBf.append("?,");
        }
        sqlBf.deleteCharAt(sqlBf.length() - 1);
        sqlBf.append(")");
        List<Object> params = new ArrayList<>();
        params.addAll(ids);

        DatabaseUtils.execSql(params, sqlBf.toString(), Integer.class);
        return 1;
    }

    @Override
    public Long getGroupCount(GroupInfoSelectDto dto) {
        Long count = this.selectCount(dto.getName(), dto.getGroupType());
        return count;
    }

    @Override
    public GroupInfoPo getById(String uuid) {
        List<Object> params = new ArrayList<>();
        StringBuffer sqlBf = new StringBuffer();
        sqlBf.append("select * from auto_flow_group_info where 1=1 and uuid = ? ");
        params.add(uuid);
        GroupInfoPo apiInstancePo = DatabaseUtils.execQueryOneSql(params, sqlBf.toString(), GroupInfoPo.class);
        return apiInstancePo;
    }

    @Override
    public int save(GroupInfoPo groupInfoPo) {
        DatabaseUtils.saveAuto("auto_flow_group_info", groupInfoPo);
        return 1;
    }

    @Override
    public int updateById(GroupInfoPo groupInfoPo) {
        DatabaseUtils.updateAutoById("auto_flow_group_info", groupInfoPo);
        return 1;
    }

    @Override
    public List<GroupInfoPo> listByIds(List<String> groupIds) {
        if (groupIds == null || groupIds.size() < 1) {
            return new ArrayList<>();
        }
        StringBuffer sqlBf = new StringBuffer();
        sqlBf.append("select * from auto_flow_group_info where 1=1 and uuid in ( ");
        for (int i = 0; i < groupIds.size(); i++) {
            sqlBf.append("?,");
        }
        sqlBf.deleteCharAt(sqlBf.length() - 1);
        sqlBf.append(")");
        List<Object> params = new ArrayList<>();
        params.addAll(groupIds);
        List<GroupInfoPo> apiInstanceNodePos = DatabaseUtils.execQueryListSql(params, sqlBf.toString(), GroupInfoPo.class);
        return apiInstanceNodePos;
    }

    @Override
    public List<GroupInfoPo> list(String name, Integer groupType) {
        List<Object> params = new ArrayList<>();
        StringBuffer sqlBf = new StringBuffer();
        sqlBf.append("select * from auto_flow_group_info where 1=1 ");
        if (StringUtils.isNotBlank(name)) {
            sqlBf.append(" and name like '%?%' ");
            params.add(name);
        }
        if (groupType != null) {
            sqlBf.append(" and group_type = ? ");
            params.add(groupType);
        }
        sqlBf.append(" order by name asc ");
        List<GroupInfoPo> apiInfoPos = DatabaseUtils.execQueryListSql(params, sqlBf.toString(), GroupInfoPo.class);
        return apiInfoPos;
    }

    @Override
    public Long selectCount(String name, Integer groupType) {
        StringBuffer sqlBf = new StringBuffer();
        List<Object> params = new ArrayList<>();
        sqlBf.append("select count(0) from auto_flow_group_info where 1=1 ");
        if (StringUtils.isNotBlank(name)) {
            sqlBf.append(" and name like '%?%' ");
            params.add(name);
        }
        if (groupType != null) {
            sqlBf.append(" and group_type = ? ");
            params.add(groupType);
        }
        Long l = DatabaseUtils.execQueryOneSql(params, sqlBf.toString(), Long.class);
        return l;
    }
}
